import { Controller } from "../../mvc/core/Controller";
import { ETaskType } from "../../../Message/Game/ETaskType";
import { ETaskSubType } from "../../../Message/Game/ETaskSubType";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { TaskInfo } from "../model/TaskInfo";
import { STask } from "../../../Message/Game/STask";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { SkillProgress } from "../view/common/SkillProgress";
import { ProcessType } from "../rules/ProcessType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { DirectionType } from "../scene/modle/data/DirectionType";
import { IHuntTaskModule } from "../../../modules/interfaces/IHuntTaskModule";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SStartTreasureHunt } from "../../../Message/Game/SStartTreasureHunt";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { EPassType } from "../../../Message/Game/EPassType";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { HuntTaskInfo } from "../model/HuntTaskInfo";
import { IHuntDialogModule } from "../../../modules/interfaces/IHuntDialogModule";
import { HuntDialogModule } from "../../../modules/HuntDialogModule";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { IView } from "../../mvc/interfaces/IView";
import { HuntTaskModule } from "../../../modules/HuntTaskModule";
type int = number;
//class HuntTaskController
    
    export  class HuntTaskController extends Controller
    {
       

        /* internal  */onTaskListRes(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                if ((loc3 = arg1[loc1]).sTask.type == ETaskType._ETaskTypeExplore && loc3.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt) 
                {
                    loc4 = loc3.sTask.processes[0];
                    cache.huntTask.updateHunMapData(loc4.contents[0], loc4.contents[1], loc4.contents[2]);
                    cache.huntTask.inHuntDistance = false;
                    cache.huntTask.hasHuntTask = true;
                    if (loc3.status != ETaskStatus._ETaskStatusNotCompleted) 
                    {
                        cache.huntTask.endHuntTask = true;
                    }
                    else 
                    {
                        cache.huntTask.endHuntTask = false;
                        if (cache.huntTask.huntTaskInfo.mapId == MapFileUtil.mapID) 
                        {
                            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                            RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                        }
                    }
                    break;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onTaskGetRes(arg1: Object): void
        {
            var loc2=null;
            if (arg1 == null || arg1.task == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1.sTask.type == ETaskType._ETaskTypeExplore && loc1.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt) 
            {
                loc2 = loc1.sTask.processes[0];
                cache.huntTask.updateHunMapData(loc2.contents[0], loc2.contents[1], loc2.contents[2]);
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.hasHuntTask = true;
                cache.huntTask.endHuntTask = false;
                if (cache.huntTask.huntTaskInfo.mapId == MapFileUtil.mapID) 
                {
                    RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                }
                if (this.huntDialogWin.isHide) 
                {
                    this.huntDialogWin.show();
                }
                this.onDialogWinShowHandler(null);
            }
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            if (arg1 == null || arg1.task == null) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1.sTask.type == ETaskType._ETaskTypeExplore && loc1.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt) 
            {
                cache.huntTask.hasHuntTask = false;
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.endHuntTask = true;
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
            }
            return;
        }

        /* internal  */onTaskCancelRes(arg1: STask): void
        {
            if (arg1.type == ETaskType._ETaskTypeExplore && arg1.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt) 
            {
                RolePlayer.instance.removeTargetTilePoint();
                cache.huntTask.hasHuntTask = false;
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.endHuntTask = false;
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                Dispatcher.dispatchEvent(new DataEvent(EventName.HuntLocationHide));
            }
            return;
        }

        /* internal  */onTaskUpdate(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                if ((loc3 = arg1[loc1]).status == ETaskStatus._ETaskStatusHadCompleted && loc3.sTask.type == ETaskType._ETaskTypeExplore && loc3.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt) 
                {
                    cache.huntTask.endHuntTask = true;
                    RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HuntLocationHide));
                    break;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onSceneUpDateHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (cache.huntTask.hasHuntTask && !cache.huntTask.endHuntTask) 
            {
                if (cache.huntTask.huntTaskInfo.mapId != MapFileUtil.mapID) 
                {
                    cache.huntTask.inHuntDistance = false;
                    loc1 = cache.task.updateHuntTaskProcess();
                    NetDispatcher.dispatchCmd(ServerCommand.TaskUpdate, loc1);
                    RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.removeTargetTilePoint();
                }
                else 
                {
                    this._huntAlertEd = false;
                    this.onGridWalkEndHandler(null);
                    RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                }
            }
            return;
        }

        /* internal  */onGridWalkEndHandler(arg1: PlayerEvent): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc6=0;
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt) 
            {
                SkillProgress.instance.dispose();
                RolePlayer.instance.huntPlayer.removeToRole();
            }
            var loc1=cache.huntTask.huntTaskInfo.px - RolePlayer.instance.currentPoint.x;
            var loc2=cache.huntTask.huntTaskInfo.py - RolePlayer.instance.currentPoint.y;
            if (Math.abs(loc1) <= 1 && Math.abs(loc2) <= 1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20121));
                RolePlayer.instance.removeTargetTilePoint();
                if (!cache.huntTask.inHuntDistance) 
                {
                    cache.huntTask.inHuntDistance = true;
                    loc3 = cache.task.updateHuntTaskProcess();
                    NetDispatcher.dispatchCmd(ServerCommand.TaskUpdate, loc3);
                }
                this._nextMsgTime = 0;
            }
            else 
            {
                if (cache.huntTask.inHuntDistance) 
                {
                    cache.huntTask.inHuntDistance = false;
                    loc5 = cache.task.updateHuntTaskProcess();
                    NetDispatcher.dispatchCmd(ServerCommand.TaskUpdate, loc5);
                }
                if ((loc4 = loc1 * loc1 + loc2 * loc2) > 2500) 
                {
                    if (this._huntAlertEd == false) 
                    {
                        this._huntAlertEd = true;
                        MsgManager.showRollTipsMsg(Language.getString(20122));
                        RolePlayer.instance.removeTargetTilePoint();
                    }
                }
                else 
                {
                    loc6 = GameMapUtil.getDirectionByXY(RolePlayer.instance.currentPoint.x, RolePlayer.instance.currentPoint.y, cache.huntTask.huntTaskInfo.px, cache.huntTask.huntTaskInfo.py);
                    this._lastDir = loc6;
                    if (loc4 >= 9 && loc4 <= 2500 && (this._lastDir == 0 || !(this._lastDir == loc6) || this._nextMsgTime == 0 || this._nextMsgTime <= flash.utils.getTimer())) 
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20123, DirectionType.getSide(loc6)));
                        this._nextMsgTime = flash.utils.getTimer() + 1000 * 1 * 5;
                    }
                    else if (loc4 >= 3 && loc4 < 9) 
                    {
                        MsgManager.showRollTipsMsg(Language.getStringByParam(20124, DirectionType.getSide(loc6)));
                        this._nextMsgTime = 0;
                    }
                    RolePlayer.instance.setTargetTilePoint(cache.huntTask.huntTaskInfo.px, cache.huntTask.huntTaskInfo.py);
                    this._huntAlertEd = false;
                }
            }
            return;
        }

        /* internal  */onItemUseHuntMap(arg1: DataEvent): void
        {
            view.show();
            (view as IHuntTaskModule).updateData(cache.huntTask.huntTaskInfo);
            return;
        }

        /* internal  */onItemUseHuntShovel(arg1: DataEvent): void
        {
            if (RolePlayer.instance.isDead) 
            {
                return;
            }
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt) 
            {
                return;
            }
            var loc1=arg1.data as ItemData;
            GameProxy.huntProxy.startTreasureHunt(loc1.uid);
            return;
        }

        /* internal  */onHuntEndHandler(): void
        {
            RolePlayer.instance.huntPlayer.hurtEnd();
            flash.utils.setTimeout(this.hideHurt, 1000);
            GameProxy.huntProxy.endTreasureHunt();
            return;
        }

        /* internal  */hideHurt(): void
        {
            RolePlayer.instance.huntPlayer.removeToRole();
            return;
        }

        /* internal  */onStartHuntRes(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SStartTreasureHunt;
            if (loc1.flag) 
            {
                RolePlayer.instance.huntPlayer.addToRole();
                RolePlayer.instance.huntPlayer.hurtStart();
                SkillProgress.instance.updateData(ProcessType.Hunt, Language.getString(20126), 5000, this.onHuntEndHandler, 2500, this.onIntervalHandler);
                SoundManager.instance.soundPlay(SoundTypeConst.WordPower);
                if (cache.pack.mountsPackCache.hadCallMount) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.UnCallMounts));
                }
            }
            return;
        }

        /* internal  */onRoleKillByEntity(arg1: MessageBlock): void
        {
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt) 
            {
                SkillProgress.instance.dispose();
                this.hideHurt();
            }
            return;
        }

        /* internal  */onIntervalHandler(): void
        {
            RolePlayer.instance.huntPlayer.hurtShow();
            return;
        }

        /* internal  */onHuntUseItemReq(arg1: DataEvent): void
        {
            var loc2=null;
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt) 
            {
                return;
            }
            var loc1=cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropHuntShovel);
            if (loc1.length > 0) 
            {
                loc2 = loc1[0];
                GameProxy.huntProxy.startTreasureHunt(loc2.uid);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(20127));
            }
            return;
        }

        /* internal  */onHunLocationReq(arg1: DataEvent): void
        {
            if (cache.huntTask.hasHuntTask) 
            {
                if (cache.huntTask.endHuntTask) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(20128));
                }
                else 
                {
                    if (!cache.vip.isVIP) 
                    {
                        if (cache.login.loginGame.money.coin + cache.login.loginGame.money.coinBind < 500) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(20038));
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, 500));
                            return;
                        }
                    }
                    GameProxy.huntProxy.hunLocationReq();
                }
            }
            return;
        }

        /* internal  */onHuntRangeConveyReq(arg1: DataEvent): void
        {
            if (!cache.vip.isVIP) 
            {
                if (cache.login.loginGame.money.coin + cache.login.loginGame.money.coinBind < 2000) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(20038));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, 2000));
                    return;
                }
            }
            GameProxy.sceneProxy.pass(EPassType.convert(EPassType._EPassTypeTreasureHelp), cache.huntTask.huntTaskInfo.mapId, cache.huntTask.huntTaskInfo.px, cache.huntTask.huntTaskInfo.py);
            view.hide();
            return;
        }

        /* internal  */onMountsCallRes(arg1: Object): void
        {
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt) 
            {
                SkillProgress.instance.dispose();
                RolePlayer.instance.huntPlayer.removeToRole();
            }
            return;
        }

        /* internal  */onDialogWinShowHandler(arg1: DataEvent): void
        {
            if (cache.dialog.npcInfo) 
            {
                if (cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupTreasure)) 
                {
                    if (!cache.dialog.dialogInfo) 
                    {
                        cache.dialog.dialogInfo = cache.dialog.getPlayerHuntTask();
                    }
                    cache.huntTask.huntTaskInfo.count = cache.daily.huntInfo.num;
                    cache.huntTask.huntTaskInfo.maxCount = cache.daily.huntInfo.totalNum;
                    this.huntDialogWin.updateData(cache.huntTask.huntTaskInfo, cache.dialog.dialogInfo, cache.dialog.npcInfo);
                    if (this.huntDialogWin.isHide) 
                    {
                        this.huntDialogWin.show();
                    }
                }
                else 
                {
                    GameProxy.huntProxy.openTreasureHunt(cache.dialog.npcInfo.tnpc.npcId);
                }
            }
            return;
        }

        /* internal  */onHuntTaskInfoRes(arg1: HuntTaskInfo): void
        {
            this.huntDialogWin.show();
            this.huntDialogWin.updateData(arg1, cache.dialog.dialogInfo, cache.dialog.npcInfo);
            return;
        }

        /* internal  */onHuntTaskGetReq(arg1: DataEvent): void
        {
            GameProxy.taskProxy.getTaskReq(cache.dialog.npcInfo.tnpc.npcId, cache.dialog.dialogInfo.sTask.code);
            return;
        }

        /* internal  */onHuntTaskEndReq(arg1: DataEvent): void
        {
            GameProxy.taskProxy.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, cache.dialog.dialogInfo.sTask.code, cache.dialog.dialogInfo.sTask.endChoose);
            return;
        }

        /* internal  */get huntDialogWin(): IHuntDialogModule
        {
            if (!this._huntDailogWin) 
            {
                this._huntDailogWin = new HuntDialogModule();
                this._huntDailogWin.addEventListener(EventName.HuntTaskGetReq, this.onHuntTaskGetReq);
                this._huntDailogWin.addEventListener(EventName.HuntTaskEndReq, this.onHuntTaskEndReq);
            }
            return this._huntDailogWin;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskCancelRes, this.onTaskCancelRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.onTaskListRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdate);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateStartTreasureHunt, this.onStartHuntRes);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillByEntity, this.onRoleKillByEntity);
            NetDispatcher.addCmdListener(ServerCommand.HuntTaskInfoRes, this.onHuntTaskInfoRes);
            NetDispatcher.addCmdListener(ServerCommand.MountsCallRes, this.onMountsCallRes);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpDateHandler);
            Dispatcher.addEventListener(EventName.ItemUse_HuntMap, this.onItemUseHuntMap);
            Dispatcher.addEventListener(EventName.ItemUse_HuntShovel, this.onItemUseHuntShovel);
            Dispatcher.addEventListener(EventName.HuntDialogShow, this.onDialogWinShowHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            var loc1=new HuntTaskModule();
            loc1.addEventListener(EventName.HuntUseItemReq, this.onHuntUseItemReq);
            loc1.addEventListener(EventName.HuntLocationReq, this.onHunLocationReq);
            loc1.addEventListener(EventName.HuntRangeConvey, this.onHuntRangeConveyReq);
            return loc1;
        }

        private /* var */_huntDailogWin: IHuntDialogModule;

        private /* var */_nextMsgTime: uint;

        private /* var */_lastDir: int;

        private /* var */_huntAlertEd: Boolean;
    }
